package com.wenhai.spring.data.binding;

import com.wenhai.xiaomage.spring.ioc.overview.dependency.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValues;
import org.springframework.validation.DataBinder;

import java.util.HashMap;
import java.util.Map;

/**
 * {@link org.springframework.validation.DataBinder} 示例
 *
 * @author wenhai
 * @date   2021/4/8
 * @see org.springframework.validation.DataBinder
 */
public class DataBinderDemo {
    public static void main(String[] args) {
        User user = new User();
        DataBinder dataBinder = new DataBinder(user, "user");
        Map<String, Object> source = new HashMap<>();
        source.put("id", 1);
        source.put("username", "文海");
        // User 不存在的值
        source.put("age", 23);
        // 嵌套
        source.put("company.name", "大地");
        PropertyValues mutablePropertyValues = new MutablePropertyValues(source);
        dataBinder.bind(mutablePropertyValues);
        System.out.println(user);

    }
}
